package org.xeustechnologies.jcksum.algo;

import org.xeustechnologies.jcksum.AbstractChecksum;

public class Bsd16 extends AbstractChecksum {

    private int checksum = 0;

    @Override
    public byte[] getBytes() {
        return new byte[] { (byte) ( ( checksum >> 8 ) & 0xff ), (byte) ( checksum & 0xff ) };
    }

    @Override
    public String getStringValue() {
        return null;
    }

    @Override
    public long getValue() {
        return checksum;
    }

    @Override
    public void reset() {
        checksum = 0;
    }

    @Override
    public void update(int b) {
        checksum = ( checksum >> 1 ) + ( ( checksum & 1 ) << 15 );
        checksum += (char) b;
        checksum &= 0xffff;
    }
}
